﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CMN.Entity;
using CMN.Common;
using System.Text.RegularExpressions;
namespace CMN.Forms
{
    public partial class frmTool : Form
    {
        public frmTool()
        {
            InitializeComponent();
            foreach (Control c in this.Controls)
            {
                if (c is TextBox)
                {
                    c.KeyDown += new KeyEventHandler(allTextBox_KeyDown);
                    c.DoubleClick += new EventHandler(allTextBox_DoubleClick);
                }
            }
        }

        private void btnDo_Click(object sender, EventArgs e)
        {
           
            btnDo.Enabled = false;
             StringBuilder sb = new StringBuilder();
             Regex rg = new Regex("con=\"(?'url'[\\s|\\S]*?)\"", RegexOptions.IgnoreCase);
             Regex rg2 = new Regex("[a-zA-Z0-9&]{1}", RegexOptions.IgnoreCase);
             string[] strs = tbUrl.Text.Split(' ');
             int index = 1;
             int notIn = 0;
             string strUrl = "http://dj.iciba.com/" + Utils.UrlEncode(tbUrl.Text) + "-1-{*}-%01-0-0.html?_=1427618395830";
             for (int i = 1; i < 1000; i++)
             {
                  string strConent="";
                
                 string strUrl2 =strUrl.Replace("{*}", i.ToString());
                 try
                 {
                     strConent = Utils.GetRequestString(strUrl2, 1600, 1, Encoding.GetEncoding("utf-8"), null);

                 }
                 catch (Exception ex)
                 {
                     string str = ex.Message;

                     strConent = "";
                     i = 1000;
                 }
                 //申明和实例化一个正则表达式对象，这里要加上参数RegexOptions.IgnoreCase，用于忽略文本大小写

                 MatchCollection matches = rg.Matches(strConent);
                 //上处可以匹配文本里有多组相似的结果全部结果，如果只有一组的话，用Match match = regex.Match(json);
                 if(matches.Count==0)
                     i = 1000;
                 string strValue = "";
                 bool isAllHas = false;
                 foreach (Match match in matches)
                 {
                     strValue = match.Groups["url"].Value.Trim();
                     if (!string.IsNullOrEmpty(strValue))
                     {
                         if (!rg2.IsMatch(strValue.Substring(0, 1)))
                         {
                             for (int j = 0; j < strs.Length; j++)
                             {
                                 if (strValue.IndexOf(strs[j]) <= -1)
                                 {
                                     isAllHas = false;
                                     notIn++;
                                     break;
                                 }
                                 else
                                     isAllHas = true;

                             }
                             if (isAllHas)
                             {
                                 sb.AppendLine(string.Concat(index.ToString(), ".", Utils.HtmlDecode(strValue)));
                                 index++;
                             }
                         }
                     }
                     isAllHas = false;
                 }
                 System.Threading.Thread.Sleep(200);
             }
            index=0;
            tbContent.Text = sb.ToString()+notIn.ToString();
            btnDo.Enabled = true;
        }
        /* 按下 Ctrl+A 全選文字 */
        private void allTextBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Modifiers == Keys.Control && e.KeyCode == Keys.A)
                ((TextBox)sender).SelectAll();
        }

        /* 雙擊全選文字 */
        private void allTextBox_DoubleClick(object sender, EventArgs e)
        {
            ((TextBox)sender).SelectAll();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            button1.Enabled = false;
            StringBuilder sb = new StringBuilder();
            Regex rg = new Regex("<tr>(?'url'[\\s|\\S]*?)</tr>", RegexOptions.IgnoreCase);
           
            string[] strs = tbUrl.Text.Split(' ');
            int endNum = Utils.StringToInt(tbNum.Text, 1);
            int notIn = 0;
            int index = 0;
            string strUrl = "http://www.ichacha.net/hy/zaoju.aspx?q=" + Utils.UrlEncode(tbUrl.Text) + "&p={*}";
            for (int i = 1; i < endNum; i++)
             {
                  string strConent="";
                
                 string strUrl2 =strUrl.Replace("{*}", i.ToString());
                 try
                 {
                     strConent = Utils.GetRequestString(strUrl2, 1600, 1, Encoding.GetEncoding("utf-8"), null);

                 }
                 catch (Exception ex)
                 {
                     string str = ex.Message;

                     strConent = "";
                     i = endNum;
                 }
                 //申明和实例化一个正则表达式对象，这里要加上参数RegexOptions.IgnoreCase，用于忽略文本大小写

                 MatchCollection matches = rg.Matches(strConent);
                 //上处可以匹配文本里有多组相似的结果全部结果，如果只有一组的话，用Match match = regex.Match(json);
                 if(matches.Count==0)
                     i = endNum;
                 string strValue = "";
                 bool isAllHas = false;
                 foreach (Match match in matches)
                 {
                     strValue = match.Groups["url"].Value.Trim();
                     strValue = Utils.RemoveHtml(strValue).Trim();
                     if (!string.IsNullOrEmpty(strValue))
                     {
                         if (strValue.StartsWith("当前位置") || strValue.StartsWith("用" + tbUrl.Text) || strValue.StartsWith("\"" + tbUrl.Text + "\"的近义词"))
                         {
                             isAllHas = false;
                         }
                         else
                         {
                             if (strValue.StartsWith("造句与例句"))
                                 strValue = strValue.Replace("造句与例句手机版","");
                             for (int j = 0; j < strs.Length; j++)
                             {
                                 if (strValue.IndexOf(strs[j]) <= -1)
                                 {
                                     isAllHas = false;
                                     notIn++;
                                     break;
                                 }
                                 else
                                     isAllHas = true;

                             }
                         }
                         if (isAllHas)
                         {
                             sb.AppendLine( Utils.HtmlDecode(strValue));
                             //sb.AppendLine(string.Concat(index.ToString(), ".", Utils.HtmlDecode(strValue)));
                             index++;
                         }

                     }
                     isAllHas = false;
                 }
                 System.Threading.Thread.Sleep(200);
                 index = 0;
                 tbContent.Text = sb.ToString() + notIn.ToString();
                 button1.Enabled = true;
             }
           
        }

        private void button2_Click(object sender, EventArgs e)
        {
            button2.Enabled = false;
            string  strIndex= ".";
            if (!string.IsNullOrEmpty(tbUrl.Text))
                strIndex = tbUrl.Text;
            Dictionary<string, string> listStrs = new Dictionary<string, string>();
            string[] strs = tbContent.Text.Split( new string[]{ "\r\n"}, StringSplitOptions.RemoveEmptyEntries);
            for (int i = 0; i < strs.Length; i++)
            {
                int indexNum = strs[i].IndexOf(strIndex);
                if (indexNum > -1)
                {
                    
                    string strTemp = strs[i].Substring(indexNum+1, strs[i].Length - indexNum-1);
                    if (strTemp.Length > 6)
                    {
                        string key = strTemp.Substring(0, 6);
                        if (!listStrs.ContainsKey(key))
                            listStrs.Add(key, strTemp);
                        else
                            indexNum = indexNum;
                    }
                }
            }
            StringBuilder sb = new StringBuilder();
            int index=1;
            foreach (string strKey in listStrs.Keys)
            {
                sb.AppendLine(string.Concat(index.ToString(),".", listStrs[strKey]));
                index++;
            }
            tbContent.Text = sb.ToString();
            button2.Enabled = true;

        }
    }
}
